<?php

/**
 * @filename source_module.php 2013-11-11 02:10:56x
 * @author Huming Xu <huming17@126.com>
 * @version 1.0.0
 * @copyright DZF (c) 2013, Huming Xu
 */
class ctrl_index_home extends index_home {

    public $info_array = array();
    public $page_array = array();
    public $tree_array = array();

    static function &instance() {
        static $object;
        if (empty($object)) {
            $object = new self();
        }
        return $object;
    }

    /*
     *  系统信息列表
     */

    public function do_index() {
        global $_G;
        $api = isset($_REQUEST['api']) ? $_REQUEST['api'] : 'json';
        
        $so_id = $this->one_so_info('user_detail', 'user_id', $_G['user_id']);
        //DEBUG 获取首页轮播图片 开始
        $page_condition = array(
            'page' => 1, //int 请求页面 页码
            'limit' => 10, //int 每页请求个数
            'perpage' => 3, //int 每页显示个数
            'wheresql' => ' AND ac_id=1 AND isdelete=0 AND ai_status=1  AND so_id ='.$so_id.' ', //string //条件SQL语句 
            'orderby' => 'ORDER BY ai_id ASC', //string 排序规则
        );
        $page_result = $this->api('ad_info', $page_condition);
        //$sql = "SELECT * FROM ".DB::table('ad_info')." WHERE ac_id=1 AND isdelete=0 AND ai_status=1 AND ai_belong=0 ORDER BY ai_sort ASC LIMIT 3";
        //$result = DB::fetch_all($sql);
        if($page_result['page_data']){       	
	        foreach ($page_result['page_data'] AS $key => $value) {
	            $tmp = array("id" => '', "color" => '', "url" => '', "img" => '', "name" => '');
	            $tmp['id'] = $value['ai_id'];
	            $tmp['color'] = $value['ai_bgcolor'];
	            $tmp['url'] = $value['ad_link'];
	            $tmp['img'] = $value['ai_path'];
	            $tmp['name'] = $value['ai_name'];
	            $data['slide'][] = $tmp;
	        }
        }else{
        	$data['slide'] = [];
        }
        //DEBUG 获取首页轮播图片 结束
        //DEBUG 获取首页广告图片 开始
        $page_condition = array(
            'page' => 1, //int 请求页面 页码
            'limit' => 5, //int 每页请求个数
            'perpage' => 5, //int 每页显示个数
            'wheresql' => ' AND ac_id IN (2,3) AND isdelete=0 AND ai_status=1  AND so_id ='.$so_id.' ', //string //条件SQL语句 
            'orderby' => 'ORDER BY ac_id ASC,ai_sort ASC', //string 排序规则
        );
        $page_result = $this->api('ad_info', $page_condition);
        //$sql = "SELECT * FROM ".DB::table('ad_info')." WHERE ac_id IN (2,3) AND isdelete=0 AND ai_status=1 AND ai_belong=0 ORDER BY ac_id ASC,ai_sort ASC LIMIT 5";
        //$result = DB::fetch_all($sql);
        if($page_result['page_data']){
	        foreach ($page_result['page_data'] AS $key => $value) {
	            $tmp = array("id" => '', "img" => '', "url" => '');
	            $tmp['id'] = $value['ai_id'];
	            $tmp['name'] = $value['ai_name'];
	            $tmp['img'] = $value['ai_path'];
	            $tmp['color'] = $value['ai_bgcolor'];
	            $tmp['url'] = $value['ad_link'];
	            $data['ad'][] = $tmp;
	        }
        }else{
        	$data['ad']=[];
        }
        //DEBUG 获取首页广告图片 结束
      
        //DEBUG 获取楼层课程 开始
        $page_condition = array(
            'page' => 1, //int 请求页面 页码
            'limit' => 20, //int 每页请求个数
            'perpage' => 4, //int 每页显示个数
            'wheresql' => " AND as_position ='floor' AND isdelete=0 AND so_id =".$so_id.' ', //string //条件SQL语句 
            'orderby' => 'ORDER BY as_id ASC', //string 排序规则
        );
        $page_result = $this->api('api_setting', $page_condition);
        $floor = $as_ids = array();
        foreach ($page_result['page_data'] AS $key => $value) {
            $tmp = json_decode($value['as_setting'], true);
            $as_ids[$value['as_id']] = $value['as_id'];
            $floor[$value['as_id']] = $tmp;
            $floor[$value['as_id']]['as_id']=$value['as_id'];
            $floor[$value['as_id']]['as_sort']=$value['as_sort'];            
        }
        $page_condition = array(
            'page' => 1, //int 请求页面 页码
            'limit' => 16, //int 每页请求个数
            'perpage' => 16, //int 每页显示个数
            'wheresql' => " AND as_id IN (" . dimplode($as_ids) . ") AND isdelete=0 ", //string //条件SQL语句 
            'orderby' => 'ORDER BY am_sort ASC', //string 排序规则
        );
        $page_result = $this->api('api_main', $page_condition);
        $cm_ids = array();
        foreach ($page_result['page_data'] AS $key => $value) {
            $tmp = array("id" => '', "title" => '', "price" => '', "teacher" => '', "times" => '', "img" => '', "am_sort" => '');
            $course_info = $this->one_so_info('course_main', 'cm_id', $value['am_data'],$so_id);
            if (!empty($course_info)) {
                $tmp = array("id" => $course_info['cm_id'], "title" => $course_info['cm_name'], "price" => $course_info['cm_price'], "teacher" => ext::user_realname($course_info['user_id']), "times" => $course_info['cm_hour_num'], "img" => $course_info['cm_img'], "am_sort" => $value['am_sort']);
                $floor[$value['as_id']]['course'][] = $tmp;
            }
        }
        foreach ($floor AS $key => $value) {
            $data['floor'][] = $value;
        }
        //DEBUG 获取楼层课程 结束      
        //DEBUG 获取公告通知 开始
        $page_condition = array(
            'page' => 1, //int 请求页面 页码
            'limit' => 10, //int 每页请求个数
            'perpage' => 3, //int 每页显示个数
            //'wheresql' => ' AND info_cateid=1006 AND isdelete=0 AND so_id ='.$so_id.' AND isfrontpage=1 ', //string //条件SQL语句 
            'wheresql' => ' AND info_cateid=1006 AND isdelete=0 AND isfrontpage=1 AND so_id = '.$so_id, //string //条件SQL语句 
            'orderby' => 'ORDER BY info_id DESC,frontpage_order ASC', //string 排序规则
        );
        $page_result = $this->api('content_info', $page_condition);
        if($page_result['page_data']){
	        foreach ($page_result['page_data'] AS $key => $value) {
	            $tmp = array("id" => '', "title" => '', "url" => '', "time" => '');
	            $tmp['id'] = $value['info_id'];
	            $tmp['title'] = $value['title'];
	            //$tmp['url']='TODO url='.$value['info_id'];
	            $tmp['time'] = date('Y-m-d', $value['create_dateline']);
	            $tmp['create_dateline'] = $value['create_dateline'];
	            $data['notice'][] = $tmp;
	        }
        }else{
        	$data['notice']=[];
        }
        
       
        	$page_condition = array(
        			'page' => 1, //int 请求页面 页码
        			'limit' => 2, //int 每页请求个数
        			'perpage' => 2, //int 每页显示个数
        			'wheresql' => " AND cm_flag =1 AND isdelete=0  and so_id = ".$so_id, //string //条件SQL语句
        			'orderby' => ' order by cm_count DESC', //string 排序规则
        	);
        	$course_spread = $this->api('course_main', $page_condition);
        	if($course_spread['page_data']){
        		foreach ($course_spread['page_data'] AS $key => $value) {
        			$tmp = array("type" => '', "startTime" => '', "title" => '', "price" => '', "school" => '', "id" => '', "img" => '');
        			$tmp['type'] = $value['cm_cate_id'];
        			$tmp['startTime'] = date('Y-m-d', $value['cm_start_time']);
        			$tmp['title'] = $value['cm_name'];
        			$tmp['price'] = $value['cm_price'];
        			$school_online = $this->one_info('school_online', 'so_id', $value['so_id']);
        			$tmp['school'] = $school_online['so_name'];
        			$tmp['id'] = $value['cm_id'];
        			if($value['cm_img']&&$value['cm_img']!='http://webapi.jxzhjy.com/'){
        				$tmp['img'] = $value['cm_img'];
        			}else{
        				$tmp['img'] = "http://webapi.jxzhjy.com/data/schoolimg/default_img.jpg";
        			}
        			$tmp['is_group'] = $value['is_group'];
        			$data['spread'][] = $tmp;
        		}
        	}else{
        		$data['spread']=[];
        	}
        
        //DEBUG 获取公告通知 结束
               
            $return['code'] = '0';
            $return['data'] = $data;
            echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
              
    }   
}

?>